package com.msb.mall.mallsearch.controller;

import com.msb.common.dto.es.SkuESModel;
import com.msb.common.exception.BizCodeEnume;
import com.msb.common.utils.R;
import com.msb.mall.mallsearch.service.ElasticSearchSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: huangxiaode
 * @Date: 2023/3/3 0003 - 03 - 03 - 18:15
 * @Description: com.msb.mall.mallsearch.controller
 * @version: 1.0
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSearchSaveController {
    @Autowired
    private ElasticSearchSaveService elasticSearchSaveService;
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels){
        Boolean b = false;
        try {
            b = elasticSearchSaveService.productStatusUp(skuESModels);
        } catch (Exception e){
            log.error("ElasticSearch商品上架错误: {}",e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b){

            return R.ok();
        }
        return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
    }

    @GetMapping("/helloworld")
    public String helloworld(){
        System.out.println("ElasticSearchSaveController helloworld");
        return "HelloWorld";
    }
}
